package com.app.controller;

import com.app.aop.has.HasRedissonLock;
import com.app.aop.has.HasRole;
import com.app.domain.bo.FilesBo;
import com.app.domain.page.Page;
import com.app.domain.vo.FilesVo;
import com.app.framework.common.R;
import com.app.service.FilesService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件管理控制器
 */
@RestController
@RequestMapping("/api/upload")
public class FilesController {

    @Resource
    FilesService filesService;

    /**
     * 文件列表
     *
     * @param bo 入参
     */
    @PostMapping("/list")
    public R<Page<FilesVo>> lists(@RequestBody FilesBo bo) {
        return R.ok(filesService.lists(bo));
    }

    /**
     * 上传文件
     *
     * @param file 文件
     * @param path 路径
     */
    @HasRole()
    @HasRedissonLock("user_upload")
    @PostMapping("/{path}")
    public R<String> uploadOne(@RequestParam("file") MultipartFile file, @PathVariable("path") String path) throws IOException {
        return R.ok(filesService.uploadOne(file, path), "上传成功");
    }

    /**
     * 删除文件
     *
     * @param fileId 文件ID
     */
    @HasRole()
    @HasRedissonLock("delete_user_upload")
    @PostMapping("/delete/{fileId}")
    public R<?> deleteOne(@PathVariable("fileId") Long fileId) {
        filesService.deleteOne(fileId);
        return R.ok(null, "删除成功");
    }

}